package cc.rengu.igas.route.common.dao.impl;

import cc.rengu.igas.route.common.dao.ChannelCallCfgRouteCustomMapper;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 智能路由客户化交易配置表操作方法实现类
 *
 * @author wangcheng
 * @version 1.0
 * @date 2020/08/02
 */
public class ChannelCallCfgRouteCustomMapperImpl implements ChannelCallCfgRouteCustomMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    @Override
    public List<ChannelCallCfg> selectChannelCallCfgInfoByMsgSrcId(String msgSrcId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectChannelCallCfgInfoByMsgSrcId");
        ChannelCallCfg channelCallCfg = new ChannelCallCfg();
        channelCallCfg.setMsgSrcId(msgSrcId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, channelCallCfg, ChannelCallCfg.class);
        if (objectList.isEmpty()) {
            rglog.warn("获取交易映射关系配置信息失败,msgSrcId:<{}>", msgSrcId);
            return null;
        }
        return objectList.stream().map(item -> (ChannelCallCfg) (item)).collect(Collectors.toList());
    }


}
